#include <stdio.h>
#include "tcp.h"

int main(void)
{
	Tcp tcp;
	Tcp work;
	string ip;
	int port = 0;

	tcp.ReuseAddress(true);
	tcp.Bind("0.0.0.0", 10000);
	tcp.Listen(1);
accept:
	tcp.Accept(work);
	work.GetPeerName(ip, port);
	work.Unblock();
	printf("peer(%s, %d)\n", ip.data(), port);

	while(1)
	{
		char buf[32] = {0};
		struct tcp_info info;
		int len = work.Recv(buf, sizeof(buf));

		if( len > 0 )
		{
			buf[ len ] = 0;
			printf("buf:%s\n", buf);
		}
		if( work.GetTcpLevel(TCP_INFO, &info, len=sizeof(info)) )
		{
			switch(info.tcpi_state)
			{
				case TCP_ESTABLISHED:
					break;
				default:
					goto accept;
			}
		}
		usleep(1000);
	}

	return 0;
}
